package com.czkt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISysRoleService;
import com.czkt.service.ISysUserService;
import com.czkt.utils.Md5Utils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-06-19
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ISysRoleService sysRoleService;

    @RequestMapping("/list")
    public Object listPage(@RequestParam(defaultValue = "1") Integer pageIndex,
                           @RequestParam(defaultValue = "2") Integer pageSize,
                           @RequestParam(defaultValue = "0") Long roleId,
                           String usrName,
                           Model model) {
        IPage<SysUserVo> page = new Page<SysUserVo>(pageIndex,pageSize);
        SysUserVo sysUserVo = new SysUserVo();


        sysUserVo.setUsrName(usrName);
        sysUserVo.setUsrRoleId(roleId);
        model.addAttribute("userPager",sysUserService.selectPageVo(page,sysUserVo));
        model.addAttribute("roles",sysRoleService.list());
        model.addAttribute("usrName",sysUserVo.getUsrName());
        model.addAttribute("roleId",roleId);
        return "user/list";
    }

    @RequestMapping("/add")
    public Object addPage(Model model) {
        model.addAttribute("roles",sysRoleService.list());
        return "user/add";
    }

    @RequestMapping("/save")
    public Object save(SysUser sysUser){
        //获取加密后的字符
        String md5pwd = Md5Utils.getMd5UsrPassword(sysUser.getUsrPassword(),"czkt",2);
        sysUser.setUsrPassword(md5pwd);
        if (sysUserService.saveOrUpdate(sysUser)){
            return "redirect:user/list";
        } else if (sysUser.getUsrId() != 0){
            return "user/edit";
        } else {
            return "redirect:user/add";
        }
    }

    @RequestMapping("/edit")
    public Object edit(Long usrId,Model model){
        model.addAttribute("user",sysUserService.getById(usrId));
        model.addAttribute("roles",sysRoleService.list());
        return "user/edit";
    }

    @RequestMapping("/del{usrId}")
    @ResponseBody
    public Map del(@PathVariable("usrId") Long usrId){
        Map map = new HashMap();
        if (sysUserService.removeById(usrId)){
            map.put("delResult","true");
        }
        return map;
    }
}